<?php
namespace App\Http\Controllers\CRM;
use App\Order;
use App\Rad_user;
use App\Rad_user_account;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;

use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;
use App\Radgroupreply;
use App\Rad_users;
use App\Rad_account;
use App\Rad_merge_data;
use DB;
use App\Rad_manage_discount;
use App\Rad_order;
use App\Line;
use App\Agent;
use App\Teamqq;

use App\Manage_user_list;
use App\Discount;
use App\Http\Controllers\Controller;
use Redis;
use App\Terrace;


class PublicController extends Controller
{

    //  在线客服列表

    public function getTeamqq()
    {
        $data = self::getQQ();
        if($data != NULL)
        {
            $qq =  Teamqq::where('manage_id',$data->manage_id)->first();
            $qq->weight =1;
            $qq->save();

        }else{

            Teamqq::where('status',1)->update(['weight'=>0]);

            $data = self::getQQ();
            $qq =  Teamqq::where('manage_id',$data->manage_id)->first();
            $qq->weight =1;
            $qq->save();
        }

        return $data['manage_id'];

    }



    static function getQQ()
    {
        $data = Teamqq::where('status',1)->where('weight',0)->orderBy('id','desc')->first(['manage_id']);

        return $data;
    }

    /**
     * @param $username
     * @param $password
     * @param $radcheck_at
     * @return mixed
     * 添加radius·用户表
     */
    static  function AddRadcheck($username,$password,$radcheck_at,$speed,$connect_num)
    {
        $where = [];
        $where['username'] =$username;
        $where['value'] =$password;
        $where['attribute'] ='Cleartext-Password';
        $where['op'] =':=';

        $account_id = Radcheck::insertGetId($where);

        //   添加到期时间
        $arr = [];
        $arr['username'] =$username;
        $arr['attribute'] ='Expiration';
        $arr['op'] =':=';
        $arr['value'] = $radcheck_at;

        Radcheck::insertGetId($arr);


        //   限速
        $res = [];
        $res['username'] = $username;
        $res['attribute'] = 'Mikrotik-Rate-Limit';
        $res['value'] = $speed;
        $res['op'] = '=';
        Radreply::insertGetId($res);


        $user_group = [];
        $user_group['username'] = $username;
        $user_group['groupname'] = 'c'.$connect_num;
        $user_group['priority'] = 1;
        Radusergroup::insertGetId($user_group);

        return $account_id;

    }

    static function checkRadiusUserGroup($connect_num)
    {
        //   用户所属组  即连接数
        $group = Radgroupcheck::where('value',$connect_num)->get()->toArray();

//        // 为空  则添加组
        if(empty($group))
        {
            //   添加组
            $group_parm = [];
            $group_parm['groupname'] = 'c'.$connect_num;
            $group_parm['attribute'] = 'Simultaneous-Use';
            $group_parm['op'] = ':=';
            $group_parm['value'] = $concent_num;
            Radgroupcheck::insertGetId($group_parm);
            //   添加组的属性

            //  属性1
            $group_attribute1= [];
            $group_attribute1['groupname'] = 'c'.$connect_num;
            $group_attribute1['attribute'] = 'Auth-Type';
            $group_attribute1['op'] = ':=';
            $group_attribute1['value'] = 'Local';


            //  属性2
            $group_attribute2= [];
            $group_attribute2['groupname'] = 'c'.$connect_num;
            $group_attribute2['attribute'] = 'Service-Type';
            $group_attribute2['op'] = ':=';
            $group_attribute2['value'] = 'Framed-User';


            //  属性3
            $group_attribute3= [];
            $group_attribute3['groupname'] = 'c'.$connect_num;
            $group_attribute3['attribute'] = 'Framed-IP-Address';
            $group_attribute3['op'] = ':=';
            $group_attribute3['value'] = '255.255.255.255';

            $group_attribute4= [];
            $group_attribute4['groupname'] = 'c'.$connect_num;
            $group_attribute4['attribute'] = 'Framed-IP-Netmask';
            $group_attribute4['op'] = ':=';
            $group_attribute4['value'] = '255.255.255.0';

            Radgroupreply::insertGetId($group_attribute1);
            Radgroupreply::insertGetId($group_attribute2);
            Radgroupreply::insertGetId($group_attribute3);
            Radgroupreply::insertGetId($group_attribute4);
        }


    }




    //可以指定前缀   生成用户ID
    static function createUuid($prefix = "")
    {
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) . '-';
        $uuid .= substr($str,8,4) . '-';
        $uuid .= substr($str,12,4) . '-';
        $uuid .= substr($str,16,4) . '-';
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }

    /**
     * @param $token
     * @return mixed
     * token   换取 用ID
     */
    static function getTokenById($token)
    {
        $user = Rad_manage::where('token',$token)->get()->toArray();
        return $user;
    }
}